#encoding:utf-8

from wtforms import StringField
from wtforms.validators import EqualTo,ValidationError,Regexp,InputRequired,length
from ..forms import BaseForm
from utils import czcache

class SignupForm(BaseForm):
    telephone = StringField(validators=[Regexp(r'1[345789]\d{9}',message='请输入正确的手机号码！')])
    sms_captcha = StringField(validators=[Regexp(r'\w{4}',message='请输入正确格式的手机验证码')])
    username = StringField(validators=[Regexp(r'.{4,16}',message='请输入正确格式的用户名！')])
    password1 = StringField(validators=[Regexp(r'.{6,20}',message='请输入正确的密码！')])
    password2 = StringField(validators=[EqualTo('password1',message='两资密码不一致')])
    graph_captcha = StringField(validators=[Regexp(r'\w{4}',message='请输入正确格式的图表验证码！')])

    def validate_sms_captcha(self,field):
        sms_captcha = field.data
        telephone = self.telephone.data
        sms_captcha_mem = czcache.get(telephone)
        if not sms_captcha_mem or sms_captcha_mem != sms_captcha.lower():
            raise ValidationError(message='请输入正确的验证码')


    def validate_graph_captcha(self,field):
        graph_captcha = field.data
        graph_captcha_mem = czcache.get(graph_captcha.lower())
        if not graph_captcha_mem:
            raise ValidationError(message='请输入正确的图形验证码')


class SigninFrom(BaseForm):
    telephone = StringField(validators=[Regexp(r'1[345789]\d{9}',message='请输入正确的手机号码！')])
    password = StringField(validators=[Regexp(r'.{6,20}',message='请输入正确的密码！')])
    remember = StringField()







